﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using deviceManage.Models;
using deviceManage.vo;
using deviceManage.Filters;

namespace deviceManage.Controllers
{
    /// <summary>
    /// 设备类型分类管理
    /// </summary>
    [CheckLoginAttribute]
    public class DeviceTypeCategoryController : Controller
    {
        private readonly DeviceCtx _context;

        public DeviceTypeCategoryController(DeviceCtx context)
        {
            _context = context;
        }

        //列表主页
        public IActionResult Index()
        {
            return View();
        }

        //ajax 获取分页数据
        public AjaxResult list(int page, int limit, String name)
        {
            AjaxResult r = new AjaxResult();
            r.Count = _context.DeviceTypeCategory
                .Where(p => p.Deleted == 0).Count();
            r.Code = 0;
            r.Data = _context.DeviceTypeCategory.Where(p => p.Deleted == 0 && ((name == null) || p.Name.Contains(name)))
                .OrderBy(p => p.SortOrder)
                .Skip((page - 1) * limit).Take(limit)
                .ToList();

            return r;
        }


        /// <summary>
        /// 进入创建页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Create()
        {
            return View();
        }


        [HttpPost]
        public AjaxResult doCreate([Bind("Name,SortOrder")] DeviceTypeCategory DeviceType)
        {
            var exist = _context.DeviceTypeCategory.Any(p => p.Name == DeviceType.Name && p.Deleted == 0);
            if (exist)
            {
                return AjaxResult.GetError("名称已经存在！");
            }

            DeviceType.Deleted = 0;
            _context.Add(DeviceType);
            _context.SaveChanges();
            return AjaxResult.GetOK("操作成功！");
        }

        /// <summary>
        /// 进入编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Edit(string id)
        {
            var DeviceType = await _context.DeviceTypeCategory.FindAsync(id);
            return View(DeviceType);
        }

        /// <summary>
        /// 提交编辑
        /// </summary>        
        /// <returns></returns>
        [HttpPost]
        public AjaxResult DoEdit(string id, string name, int sortOrder)
        {
            var exist = _context.DeviceTypeCategory.Any(p => p.Name == name && p.Deleted == 0 && p.Id != id);
            if (exist)
            {
                return AjaxResult.GetError("名称已经存在！");
            }

            DeviceTypeCategory DeviceTypeCategory = this._context.DeviceTypeCategory.Single(p => p.Id == id);
            DeviceTypeCategory.Deleted = 0;
            DeviceTypeCategory.SortOrder = sortOrder;
            DeviceTypeCategory.Name = name;
            this._context.SaveChanges(); //保存

            return AjaxResult.GetOK("保存成功！");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AjaxResult Delete(string id)
        {
            AjaxResult result = new AjaxResult();
            if (id == null) return AjaxResult.GetError("删除失败！");

            result.Code = 1;
            var DeviceType = _context.DeviceTypeCategory.FirstOrDefault(m => m.Id == id);
            if (DeviceType == null) return AjaxResult.GetError("未找到数据");

            DeviceType.Deleted = 1;
            _context.SaveChanges();
            return AjaxResult.GetOK("删除成功");
        }

        private bool DeviceTypeExists(string id)
        {
            return _context.DeviceType.Any(e => e.Id == id);
        }
    }
}
